// pages/password/password.js
let utils = require("../../utils/utils.js");
const app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		walletInfo: {
			record: false,
			dateShow: false,
			totalPrice: ''
		},
		dateInfo: {
			minHour: 10,
			maxHour: 20,
			minDate: new Date(),
			maxDate: new Date(2019, 10, 1),
			currentDate: new Date()
		},
		getCashPledgeListUrl: '/myCashPledge/cashPledgeList',
		refundCashPledgeUrl: '/myCashPledge/refundCashPledge',
		isCashPledgeUrl: '/myCashPledge/isCashPledge',
		mycashPledgeUrl: '/myCashPledge/mycashPledge',
		currentDate: new Date(),
		minDate: new Date(),
		show: true,
		depositInfo: {
			mydeposit: '',
			canborrow: ''
		},
		isReturn: false,
		isActive: true,
		nonOrder: false,
		noneCash: false,
		nodata: false,
		cashlistnum: [],
		/* 判断每次加载数据长度小于10阻止加载 */
		morecashlist: false,
		isShow: false,
		cashlist: [],
		emptyLastImg: '/none-account.png',
		cardBglastImg: '/walletbg.png'
	},


	getDepositData: function() {
		let that = this;
		let indexData = wx.getStorageSync('indexData');
		let _data = {
			url: that.data.mycashPledgeUrl,
			methods: 'post',
			nonelist: true,
			data: {
				userId: indexData.user.userId,
				type: indexData.user.type
			}
		};
		utils.request.call(_data, (res) => {
			console.log(res)
			let data = res.data.data;
			console.log(res.data.data);
			let availableAntecedentMoney = res.data.data.user.availableAntecedentMoney;
			let cashPledge = res.data.data.cashPledge;
			if (!availableAntecedentMoney) {
				that.data.depositInfo.mydeposit = '暂无押金';
				that.data.isReturn = false;
				that.data.noneCash = true;
			} else {
				that.data.depositInfo.mydeposit = availableAntecedentMoney;
				that.data.isReturn = true;
			};
			if (!cashPledge) {
				that.data.depositInfo.canborrow = 0;
			} else {
				if (cashPledge.count) {
					that.data.depositInfo.canborrow = cashPledge.count;
				} else {
					that.data.depositInfo.canborrow = 0;
				};
			};
			that.setData({
				depositInfo: that.data.depositInfo,
				isReturn: that.data.isReturn,
				noneCash: that.data.noneCash
			})
			console.log(that.data.isReturn)
		});
	},

	getListData: function(offset) {
		let that = this;
		let indexData = wx.getStorageSync('indexData');
		let _data = {
			url: that.data.getCashPledgeListUrl,
			methods: 'post',
			nonelist: true,
			data: {
				userId: indexData.user.userId,
				type: indexData.user.type,
				offset: offset
			}
		};
		utils.request.call(_data, (res) => {
			console.log(res)
			let cashlist = res.data.data.content;
			that.data.cashlistnum = cashlist;
			if (cashlist.length == 0) {
				if (offset == 0) {
					that.data.cashlist = cashlist;
					that.setData({
						isShow: true,
						cashlist: cashlist,
						nodata: true
					})
				} else {
					let cashlistmore = that.data.cashlist.concat(cashlist);
					that.data.cashlist = cashlistmore;
					that.setData({
						isShow: true,
						cashlist: cashlistmore,
						morecashlist: true
					})
				}
			} else {
				if (offset == 0) {
					that.data.cashlist = cashlist;
					that.setData({
						isShow: true,
						cashlist: cashlist
					})
				} else {
					let cashlistmore = that.data.cashlist.concat(cashlist);
					that.data.cashlist = cashlistmore;
					that.setData({
						isShow: true,
						cashlist: cashlistmore,
						morecashlist: true
					})
				}
			}
		});
	},
	backAmount: function() {
		let that = this;
		let indexData = wx.getStorageSync('indexData');
		let _data = {
			url: that.data.isCashPledgeUrl,
			methods: 'post',
			nonelist: true,
			data: {
				userId: indexData.user.userId,
				type: indexData.user.type,
				deviceId: indexData.user.deviceCode
			}
		};
		utils.request.call(_data, (res) => {
			console.log(res)
			let data = res.data.data;
			utils.confirm(data.msg, (res) => {
				if (data.flag) {
					return false;
				} else {
					let indexData = wx.getStorageSync('indexData');
					console.log(indexData)
					let _data = {
						url: that.data.refundCashPledgeUrl,
						methods: 'post',
						nonelist: true,
						data: {
							userId: indexData.user.userId,
							type: indexData.user.type
						}
					};
					utils.request.call(_data, (res) => {
						console.log(res)
						if (res.data.status == 200) {
							that.data.isReturn = false;
							utils.confirm('退款请求已提交，请留意微信官方退款通知', () => {
								let offset = this.data.offset = 0;
								this.getListData(offset)
								that.getDepositData();
								that.setData({
									isReturn: that.data.isReturn
								})
							});
						} else {
							utils.tips(res.data.msg);
						}
					});
				};
			});
		});
	},


	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		this.getDepositData()
		let offset = this.data.offset = 0;
		this.getListData(offset);
		this.img();
	},
	img() {
		let emptyImg = app.globalData.baseimg + this.data.emptyLastImg;
		let cardBgImg = app.globalData.baseimg + this.data.cardBglastImg;
		this.setData({
			emptyImg,
			cardBgImg,
		});
	},
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
		let that = this;
		let offset = that.data.offset;
		let cashlistnum = that.data.cashlistnum;
		if (that.data.cashlistnum.length < 10) {
			that.setData({
				morecashlist: true
			});
			return false;
		}
		offset++;
		that.data.offset = offset;
		that.getListData(offset)
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
